-- 转轴(一列)，含有有多个Symbol
local Config = require("app.views.slots.Config")
local SymbolCfg = Config.SymbolCfg
local ReelNode = class("ReelNode", cc.Node)
local cellSize = Config.CellSize

function ReelNode:ctor(col)
    self._col = col -- 当前转轴处于第几列
    self._symbols = {} -- 所有symbol节点
    self._height = cellSize.height * Config.H -- 转轴的总高度
    self._speed = 0
    self._state = "idle"
end

function ReelNode:initWithColData(colData)
    for i, tid in ipairs(colData) do
        local cfg = SymbolCfg[tid]
        local node = require("app.views.slots.SymbolNode").new(tid)
        self:addChild(node)
        table.insert(self._symbols, node)
    end
    self:resetPos()
end

function ReelNode:insertNewData(colData)
    local firstNode = self._symbols[1]
    local y = firstNode and firstNode:getPositionY() or -self._height
    for i = #colData, 1, -1 do
        local tid = colData[i]
        local node = require("app.views.slots.SymbolNode").new(tid)
        self:addChild(node)
        node:setPositionY(y + node:getSize().height)
        y = y + node:getSize().height
        table.insert(self._symbols, 1, node)
    end
end

function ReelNode:fill(colData)
    local firstNode = self._symbols[1]
    local y = 0
    for i = #colData, 1, -1 do
        local tid = colData[i]
        local node = require("app.views.slots.SymbolNode").new(tid)
        self:addChild(node)
        node:setPositionY(y + node:getSize().height)
        y = y + node:getSize().height
        table.insert(self._symbols, 1, node)
    end
end

function ReelNode:update(dt)
    for i, node in ipairs(self._symbols) do
        node:update(dt)
    end

    if self._state == "scrolling" then
        self:moveSymbols(dt * self._speed)
        -- 上下边界如果有空隙，需要填充symbol
        self:_fillTopRandomSymbols()
        self:_fillBotRandomSymbols()

        -- 最后一个symbol的y坐标小于一定程度时，销毁
        self:_destoryInvisibleSymbols()
    elseif self._state == "stopping" then
        self._speed = self._speed + dt * 500
        self:moveSymbols(dt * self._speed)
        self:_destoryInvisibleSymbols()

        local firstNode = self._symbols[1]
        if firstNode:getPositionY() < 0 or self._speed > 0 then
            self:resetPos()
            self._state = "bouncing"
            self._speed = 100
        end
    elseif self._state == "bouncing" then
        self._speed = self._speed - dt * 500
        self:moveSymbols(dt * self._speed)
        local firstNode = self._symbols[1]
        if firstNode:getPositionY() < 0 then
            self:resetPos()
            self._state = "idle"
            self._speed = 0
        end
    end
end

-- 转轴转动
function ReelNode:moveSymbols(distance)
    for _, node in ipairs(self._symbols) do
        node:setPositionY(node:getPositionY() + distance)
    end
end

function ReelNode:setScrollSpeed(speed)
    self._speed = speed
    self._state = "scrolling"
end

-- 减速，停止转动
function ReelNode:stopScroll()
    self._speed = -2000
    -- 填充最新的服务器数据
    local initData = global.data.initData.init
    self:insertNewData(initData[self._col])
    self._state = "stopping"
end

-- 强制对齐坐标
function ReelNode:resetPos()
    local y = 0
    for _, node in ipairs(self._symbols) do
        node:setPositionY(y)
        y = y - node:getSize().height
    end
    self:_destoryInvisibleSymbols()
end

function ReelNode:_fillTopRandomSymbols()
    while true do
        local firstNode = self._symbols[1]
        if firstNode:getPositionY() < 0 then
            local fakeTid = math.random(1, #SymbolCfg) -- 生成一个随机的Symbol
            local node = require("app.views.slots.SymbolNode").new(fakeTid)
            node:setFake(true)
            self:addChild(node)
            table.insert(self._symbols, 1, node)
            node:setPositionY(firstNode:getPositionY() + node:getSize().height)
        else
            break
        end 
    end
end

function ReelNode:_fillBotRandomSymbols()
    while true do
        local lastNode = self._symbols[#self._symbols]
        if lastNode:getPositionY() - lastNode:getSize().height > - self._height  then
            local fakeTid = math.random(1, #SymbolCfg) -- 生成一个随机的Symbol
            local node = require("app.views.slots.SymbolNode").new(fakeTid)
            node:setFake(true)
            self:addChild(node)
            table.insert(self._symbols, node)
            node:setPositionY(lastNode:getPositionY() - lastNode:getSize().height)
        else
            break
        end 
    end
end

function ReelNode:_destoryInvisibleSymbols()
    while true do
        local lastNode = self._symbols[#self._symbols]
        if lastNode:getPositionY() < -self._height - cellSize.height then
            lastNode:removeFromParent(true)
            self._symbols[#self._symbols] = nil
        else
            break
        end
    end
end

function ReelNode:isStopped()
    return self._state == "idle"
end

function ReelNode:getSymbols()
    return self._symbols
end

-- 填充下落的目标位置
function ReelNode:updateSymbolsTargetPos()
    local y = -self._height
    for i = #self._symbols, 1, -1 do
        local node = self._symbols[i]
        if not node:isFake() then
            node:setStartDrop(y + node:getSize().height)
            y = y + node:getSize().height
        end
    end
end

return ReelNode